package com.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.Employee;
import com.pojo.view.StaffAnnualLeave;
import com.service.DepartmentService;
import com.service.EmployeeService;
import com.service.impl.DepartmentServiceImpl;
import com.service.impl.EmployeeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/adminServlet/*")
public class AdminServlet extends BaseServlet {
    EmployeeService employeeService = new EmployeeServiceImpl();
    DepartmentService departmentService = new DepartmentServiceImpl();

    /**
     * 查看全体员工年假天数
     * API接口说明
     * GET方法 无参数
     * 返回结果示例:
     * [
     *     {
     *         "annualLeaveRemain": 14,
     *         "annualLeaveTotal": 20,
     *         "department": "技术部",
     *         "name": "zhangsan",
     *         "workId": "100005"
     *     },
     *     {
     *         "annualLeaveRemain": 18,
     *         "annualLeaveTotal": 25,
     *         "department": "营销部",
     *         "name": "lisi",
     *         "workId": "100010"
     *     }
     * ]
     */
    public void checkStaffAnnualLeave(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Employee> employeeList = employeeService.getAllEmployee();
        List<StaffAnnualLeave> staffAnnualLeaveList = new ArrayList<>();

        for (Employee employee : employeeList) {
            StaffAnnualLeave staffAnnualLeave = new StaffAnnualLeave();

            String workId = employee.getWorkId();
            String name = employee.getName();
            Integer deptId = employee.getDeptId();
            String deptName = departmentService.selectDeptNameById(deptId);
            Integer annualLeaveTotal = employee.getAnnualLeaveTotal();
            Integer annualLeaveRemain = employee.getAnnualLeaveRemain();

            staffAnnualLeave.setWorkId(workId);
            staffAnnualLeave.setName(name);
            staffAnnualLeave.setDepartment(deptName);
            staffAnnualLeave.setAnnualLeaveTotal(annualLeaveTotal);
            staffAnnualLeave.setAnnualLeaveRemain(annualLeaveRemain);

            staffAnnualLeaveList.add(staffAnnualLeave);
        }

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        String jsonString = JSON.toJSONString(staffAnnualLeaveList);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);

    }


    /**
     * 设置年假总天数
     * API接口说明
     * POST方法
     * {"work_id":"100005", "total_day":"20"}
     * 返回结果:"set annualLeave totalDay successfully"
     */
    public void setAnnualLeaveTotal(HttpServletRequest request, HttpServletResponse response) throws IOException {
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String workId = jsonObject.get("work_id").toString();
        int totalDay = Integer.parseInt(jsonObject.get("total_day").toString());

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        employeeService.setAnnualLeaveTotalByWorkId(workId, totalDay);
        response.getWriter().write("set annualLeave totalDay successfully");
    }

    public void setAnnualLeaveRemain(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String workId = request.getParameter("work_id");
        int reduceDay = Integer.parseInt(request.getParameter("reduce_day"));

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        employeeService.setAnnualLeaveRemainByWorkId(workId, reduceDay);
        response.getWriter().write("set annualLeave remainDay successfully");
    }

    /**
     * 添加新员工
     * API接口说明
     * POST方法 接受JSON参数
     * {"name":"wangwu", "phone_number":"18005881588", "work_id":"100020", "dept_name":"技术部"}
     * 添加成功返回结果:add employee successfully
     * 添加不成功返回结果:add employee unsuccessfully
     */
    public void addEmployee(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();
        JSONObject jsonObject = JSON.parseObject(params);

        String name = jsonObject.get("name").toString();
        String phoneNumber = jsonObject.get("phone_number").toString();
        String workId = jsonObject.get("work_id").toString();
        String deptName = jsonObject.get("dept_name").toString();
        int deptId = departmentService.selectIdByDeptName(deptName);

        Employee employee = new Employee();
        employee.setName(name);
        employee.setWorkId(workId);
        employee.setPhoneNumber(phoneNumber);
        employee.setDeptId(deptId);

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        try {
            employeeService.addEmployee(employee);
            response.getWriter().write("add employee successfully");
        } catch (Exception e) {
            response.getWriter().write("add employee unsuccessfully");
        }
    }
}
